﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using FaceSharp.Api.GraphApi;
using FaceSharp.Api.Types;
using FaceSharp.Api.Common;
using Newtonsoft.Json.Linq;

namespace FaceSharp.Api.Objects
{
    public class Events : GraphBase
    {
        public Events(IFacebookCore facebookCore): base(facebookCore)
        {

        }


        public IEnumerable<Types.Event> List(string objectId)
        {
            Trace.Write("Begin Request", "Facebook.Objects.Event.List");
            var jObject = MakeGraphRequest(objectId,"events");

            var events = new List<Types.Event>();
            if (jObject != null)
            {
                var jData = (JArray)jObject["data"];
                events = (from fbEvent in jData
                         select ParseEvent(fbEvent) 
                         ).ToList();
            }
            Trace.Write("End Request", "Facebook.Objects.Event.List");
            return events;
        }

        public Types.Event Get(long eventId)
        {
            Trace.Write("Begin Request", "Facebook.Objects.Event.Get");
            var jObject = MakeGraphRequest(eventId.ToString());
            var fbevent = ParseEvent(jObject);
            Trace.Write("End Request", "Facebook.Objects.Event.Get");
            return fbevent;
        }

        private static Types.Event ParseEvent(JToken jObject)
        {
            Trace.Write("Begin Request", "Facebook.Objects.Event.ParseEvent");
            
            var fbevent = new Types.Event();
            if (jObject != null)
            {
                Trace.Write("JSONData:" + jObject.ToString(), "Facebook.Objects.Event.ParseEvent");
                
                fbevent = new Types.Event()
                {
                    Id = long.Parse((string)jObject["id"]),
                    Name = (string)jObject["name"] ?? "",
                    Description = (string)jObject["description"] ?? "",
                    StartTime = JTokenUtil.ParseDateTime(jObject,"start_time"),
                    EndTime = JTokenUtil.ParseDateTime(jObject, "end_time"),
                    Location = (string)jObject["location"] ?? "",
                    Privacy = (string)jObject["privacy"] ?? "",
                    UpdatedTime = JTokenUtil.ParseDateTime(jObject, "updated_time"),
                    RSVPStatus = (string) jObject["rsvp_status"] ?? ""
                };

                var owner = (JObject)jObject["owner"];
                if(owner != null)
                {
                    fbevent.Owner = new Types.Event.OwnerData()
                    {
                        Id = long.Parse((string)owner["id"]),
                        Name = (string)owner["name"]
                    };
                }
                
                var venue = (JObject)jObject["venue"];
                if(venue != null)
                {
                    fbevent.Venue = new Types.Event.VenueData()
                    {
                        Street = (string)venue["street"],
                        City = (string)venue["city"],
                        State = (string)venue["state"],
                        Country = (string)venue["country"],
                        Latitude = venue["latitude"].ToString(),
                        Longitude = venue["longitude"].ToString()
                    };  
                }
                
            }
            return fbevent;
        }
   
    }
}
